package com.imranstechnology.homeexpensemanager;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.imranstechnology.homeexpensemanager.utils.DateUtil;
import com.imranstechnology.homeexpensemanager.utils.StringUtil;

public final class ExpenseMemStore {
	public static final String RECORD_SEPERATOR = ";";
	public static final String STORED_YEARS_KEY = "expenseStoredYears";
	
	public static final ExpenseEntity EXPENSEE_ENTITY = new ExpenseEntity();
	public static boolean isInitalized = false;
	public static void initExpenseEntityFromPrefs(SharedPreferences preferences){
		float vMonthlyExpense = preferences.getFloat(""+R.id.monthlyExpense, 0f);
		float vTotalExpense = preferences.getFloat(""+R.id.totalExpense, 0f);
		float vDailyExpense = preferences.getFloat(""+R.id.todaysExpense, 0f);
		float vYearlyExpense = preferences.getFloat(""+R.id.yearExpense, 0f);
		String vLastSavingDate = preferences.getString(""+R.id.dateTime, null);
		String vSavedYears = preferences.getString(STORED_YEARS_KEY, null);
//		EXPENSEE_ENTITY.setDailyExpense(vDailyExpense);
//		EXPENSEE_ENTITY.setMonthlyExpense(vMonthlyExpense);
//		EXPENSEE_ENTITY.setYearlyExpense(vYearlyExpense);
//		EXPENSEE_ENTITY.setTotalExpense(vTotalExpense);
//		EXPENSEE_ENTITY.setLastSavingDate(DateUtil.stringToDate(vLastSavingDate));
		EXPENSEE_ENTITY.setLastSavingDate(DateUtil.getCurrentDate());
		if(!StringUtil.isEmpty(vSavedYears))
		{
			String years [] = vSavedYears.split(RECORD_SEPERATOR);	
			for(String year:years){
				Set<String> yearResult = preferences.getStringSet(year,new HashSet<String>(0));
				for(String rec:yearResult){
					ExpenseDetailEntity detEntity = ExpenseDetailEntity.parseExpenseDetailRecord(rec);
//					EXPENSEE_ENTITY.addDetail(Integer.parseInt(year),detEntity);
					addExpense(detEntity.getExpenseAmount(), detEntity.getTransactionDate(), detEntity.getExpenseCategory());
				}
			}
		}
		fixDailyAmounts();
		isInitalized = true;
	}
	public static void fixDailyAmounts(){
		Date today = DateUtil.getCurrentDate();
		if(!DateUtil.isSameDay(today, EXPENSEE_ENTITY.getLastSavingDate())){
			EXPENSEE_ENTITY.setDailyExpense(0f);
		}
		if(!DateUtil.isSameMonth(today, EXPENSEE_ENTITY.getLastSavingDate())){
			EXPENSEE_ENTITY.setMonthlyExpense(0f);
		}
		if(!DateUtil.isSameYear(today, EXPENSEE_ENTITY.getLastSavingDate())){
			EXPENSEE_ENTITY.setYearlyExpense(0f);
		}

	}
	public static void addExpense(float dailyExpense, final Date expDate1, final Integer category){
		Date currDate = DateUtil.getCurrentDate();
		currDate = DateUtil.truncateDate(currDate);
		Date expDate = DateUtil.truncateDate(expDate1);
		Date lastDate = EXPENSEE_ENTITY.getLastSavingDate();
		lastDate = DateUtil.truncateDate(lastDate);
		if(DateUtil.isSameYear(currDate, expDate))
		{
			if(DateUtil.isSameDay(expDate, lastDate)){
				EXPENSEE_ENTITY.setDailyExpense(EXPENSEE_ENTITY.getDailyExpense() + dailyExpense);
			}
			else 
			{
				if(DateUtil.isSameMonth(expDate, currDate) && DateUtil.isSameDay(expDate, currDate))
					EXPENSEE_ENTITY.setDailyExpense(dailyExpense);			
			}
			if(DateUtil.isSameMonth(expDate, lastDate))
			{
				EXPENSEE_ENTITY.setMonthlyExpense(EXPENSEE_ENTITY.getMonthlyExpense() + dailyExpense);
			}else
			{
				if(DateUtil.isSameMonth(expDate, currDate))
					EXPENSEE_ENTITY.setMonthlyExpense(dailyExpense);
			}
			if(DateUtil.isSameYear(expDate, lastDate))
			{
				EXPENSEE_ENTITY.setYearlyExpense(EXPENSEE_ENTITY.getYearlyExpense() + dailyExpense);
			}else
			{
				EXPENSEE_ENTITY.setYearlyExpense(dailyExpense);
			}
		}
		EXPENSEE_ENTITY.setTotalExpense(EXPENSEE_ENTITY.getTotalExpense() + dailyExpense);
		EXPENSEE_ENTITY.setLastSavingDate(currDate);

		
		if(DateUtil.isSameYear(currDate, expDate))
		{
			int size = EXPENSEE_ENTITY.getYearRecords(DateUtil.getCurrentYear()).size();
			EXPENSEE_ENTITY.addDetail(DateUtil.getCurrentYear(), new ExpenseDetailEntity(new Long(size), expDate, category, dailyExpense));
		}else
		{
			int size = EXPENSEE_ENTITY.getYearRecords(DateUtil.getDateFiled(expDate, Calendar.YEAR)).size();
			EXPENSEE_ENTITY.addDetail(DateUtil.getDateFiled(expDate,Calendar.YEAR), new ExpenseDetailEntity(new Long(size), expDate, category, dailyExpense));
		}
	}
	public static void storeMemExpense(Editor editor){
		editor.clear();
		editor.putFloat(""+R.id.totalExpense, EXPENSEE_ENTITY.getTotalExpense());
		editor.putFloat(""+R.id.yearExpense, EXPENSEE_ENTITY.getYearlyExpense());
		editor.putFloat(""+R.id.monthlyExpense, EXPENSEE_ENTITY.getMonthlyExpense());
		editor.putFloat(""+R.id.todaysExpense, EXPENSEE_ENTITY.getDailyExpense());
		editor.putString(""+R.id.dateTime, ""+DateUtil.dateToString(EXPENSEE_ENTITY.getLastSavingDate()));
		String years = "";
		for(Integer year:EXPENSEE_ENTITY.getYearsTrnx().keySet()){
			editor.putStringSet(""+year, EXPENSEE_ENTITY.getYearRecordSet(year));
			years += ""+year+";";
		}
		years = years.substring(0,years.length()-1);
		editor.putString(STORED_YEARS_KEY,years);
		editor.commit();
	}
}
